import { DataTypes, Model } from 'sequelize'
import { sequelize } from '../../connect'
import { findAllValues, findOneValue, findOneRandomValue } from '../../public'

type ModelProps = {
  id: number
  team_id: string
  members: string[] // 可以存储成员uid数组
  leader: string // 队长uid
  player_1?: string // 1号位uid
  player_2?: string // 2号位uid
  player_3?: string // 3号位uid
  player_4?: string // 4号位uid
  player_5?: string // 5号位uid
  player_6?: string // 6号位uid
  target: string // 目标描述
}

class team extends Model<ModelProps> {
  public static findAllValues = findAllValues
  public static findOneValue = findOneValue
  public static findOneRandomValue = findOneRandomValue
}

team.init(
  {
    id: {
      type: DataTypes.BIGINT,
      primaryKey: true,
      allowNull: false,
      autoIncrement: true,
      comment: '自增主键'
    },
    team_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      unique: true,
      comment: '队伍编号'
    },
    members: {
      type: DataTypes.JSON,
      allowNull: false,
      comment: '成员uid数组'
    },
    leader: {
      type: DataTypes.INTEGER,
      allowNull: false,
      comment: '队长uid'
    },
    player_1: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: '1号位uid'
    },
    player_2: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: '2号位uid'
    },
    player_3: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: '3号位uid'
    },
    player_4: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: '4号位uid'
    },
    player_5: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: '5号位uid'
    },
    player_6: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: '6号位uid'
    },
    target: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: '目标'
    }
  },
  {
    sequelize,
    tableName: 'team',
    freezeTableName: true,
    createdAt: false,
    updatedAt: false,
    indexes: [
      {
        unique: true,
        fields: ['id']
      },
      {
        unique: true,
        fields: ['team_id']
      }
    ]
  }
)

export { team }
